import { Constant } from "../constan";
// import i18n from "i18next";
interface ActionProps {
  type: typeof Constant.ADD_LANGUAGE | typeof Constant.LANGUAGE_CODE;
  payload: any;
}

export interface LanguageProps {
  language: "en" | "zh" | "jp";
  languageList: Array<{
    name: string;
    code: string;
  }>;
}

const defaultState: LanguageProps = {
  language: "zh",
  languageList: [
    {
      name: "中文",
      code: "zh",
    },
    {
      name: "English",
      code: "en",
    },
  ],
};

export const languageReducer = (
  prevState = defaultState,
  action: ActionProps
) => {
  const { type, payload } = action;
  switch (type) {
    case Constant.LANGUAGE_CODE:
      // i18n.changeLanguage(payload); // 切换语言，但是此处处理不规范，原因：reducer是纯函数，但是此行代码会带来副作用
      const newState = {
        ...prevState,
        language: payload,
      };
      return newState;
    case Constant.ADD_LANGUAGE:
      const newlanguageList = {
        ...prevState,
        languageList: [...prevState.languageList, payload],
      };
      return newlanguageList;
    default:
      return prevState;
  }
};
